/**
 * 
 */
package com.seascape.swms.framework.web;

import java.util.List;

import javax.validation.Valid;

import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import com.seascape.swms.framework.model.PageParam;
import com.seascape.swms.framework.model.Result;
import com.seascape.swms.framework.service.BaseService;

import io.swagger.annotations.ApiOperation;

/**
 * @author Lay
 *
 */
public abstract class BaseController<T> {
		
	protected abstract BaseService<T> getBaseService();

	@ApiOperation("查询")
	@GetMapping
	public Result list(T model, Long currentPage, Long pageSize) {
		if(pageSize==null) {
			return Result.success(getBaseService().template(model));
		}
		PageParam<T> pageParam = new PageParam<T>(currentPage, pageSize, model);
		return Result.success(getBaseService().templatePage(pageParam));
	}

	@ApiOperation("创建")
	@PostMapping
	public Result create(@Valid @RequestBody T model) {
		getBaseService().create(model);
		return Result.success();
	}

	@ApiOperation("修改")
	@PutMapping("/{model.id}")
	public Result update(@RequestBody T model) {
		getBaseService().updateTemplate(model);
		return Result.success();
	}

	@ApiOperation("根据ID查询")
	@GetMapping("/{id}")
	public Result get(@PathVariable Long id) {
		return Result.success(getBaseService().queryById(id));
	}
	
	@ApiOperation("删除")
	@DeleteMapping("/{id}")
	public Result delete(@PathVariable Long id) {
		getBaseService().delete(id);
		return Result.success();
	}

	@ApiOperation("批量删除")
	@DeleteMapping("/batchdelete")
	public Result batchDelete(@RequestParam("datas") List<Long> ids) {
		for (Long id : ids) {
			getBaseService().delete(id);
		}
		return Result.success();
	}
}
